package com.zhs.gccs.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.annimon.stream.Stream;
import com.zhs.gccs.R;
import com.zhs.gccs.model.CaDict;
import com.zhs.gccs.model.StatisticsDict;

import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/18-11:07
 * Email: renweiwei@ufashion.com
 */

public class GccsLevelTypeAdapter extends RecyclerView.Adapter<GccsLevelTypeAdapter.ViewHolder> {
    private Context mContext;
    private List<StatisticsDict> datas = null;


    public GccsLevelTypeAdapter(Context context, List<StatisticsDict> datas) {
        this.datas = datas;
        this.mContext = context;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_gccs_level_item,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        StatisticsDict data = datas.get(position);
        if(data.isSelected()){
            viewHolder.tv_title.setTextColor(mContext.getResources().getColor(R.color.white));
            viewHolder.ll_item.setBackground(mContext.getDrawable(R.drawable.blue_all_rounded_border));
        }else {
            viewHolder.tv_title.setTextColor(mContext.getResources().getColor(R.color.black_333333));
            viewHolder.ll_item.setBackground(mContext.getDrawable(R.drawable.grey_all_rounded_border));
        }
        viewHolder.tv_title.setText(data.getDictName());//
        if(CaDict.Type_form.equals(data.getDictType())){
            if(data.getRequiredDicts()!=null&&data.getRequiredDicts().size()>0){
                viewHolder.tv_required.setVisibility(View.VISIBLE);
            }else {
                viewHolder.tv_required.setVisibility(View.INVISIBLE);
            }
        }else {
            viewHolder.tv_required.setVisibility(View.INVISIBLE);
        }
        if(data.getDictCount()>0){
            viewHolder.tv_count.setText(data.getDictCount()+"");//
            viewHolder.tv_count.setVisibility(View.VISIBLE);
            if(data.getRequiredDicts()!=null
                    &&data.getDictDistinctRequiredCount()<data.getRequiredDicts().size()){
                viewHolder.tv_count.setBackgroundResource(R.drawable.count_state_yellow_round);
                viewHolder.tv_count.setTextColor(mContext.getResources().getColor(R.color.black));
            }else {
                viewHolder.tv_count.setBackgroundResource(R.drawable.count_state_blue_round);
                viewHolder.tv_count.setTextColor(mContext.getResources().getColor(R.color.white));
            }
        }else {
            viewHolder.tv_count.setText("");//
            viewHolder.tv_count.setVisibility(View.GONE);
        }
        viewHolder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Stream.of(datas).forEach(item->{
                    item.setSelected(false);
                });
                data.setSelected(true);
                notifyDataSetChanged();
                if(operationListener!=null)operationListener.onItemClick(position);
            }
        });
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
        return datas!=null?datas.size():0;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View ll_item;
        public TextView tv_required;//
        public TextView tv_title;
        public TextView tv_count;
        public ViewHolder(View view){
            super(view);
            ll_item =   view.findViewById(R.id.ll_item);
            tv_required =   view.findViewById(R.id.tv_required);
            tv_title =   view.findViewById(R.id.tv_title);
            tv_count =   view.findViewById(R.id.tv_count);
        }
    }
    public static interface OperationListener{
        void onItemClick(int position);
    }
    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
